$authr()
require('../css/courseTrain.less')

require('../font/iconfont.css')

// 页面控制
require('./common/runCtrl.js')

const axios = require('../lib/axios/axios')


document.ready(function () {
    $footerClick()




    const uid = JSON.parse(localStorage.getExpire('data')).user.userId

    // 获取数据
    axios.get(`${BASE_URL}/sports/courseList?id=${uid}`).then(result => {

        result = result.data
        if (result.status === 0) {
            if (result.data.length <= 0) {

                $createMask("icon-dingwei1", "当前暂无课程请添加课程!", 1000)

            } else {
                let courseArr = result.data
                // 找到新课程
                newCourseArr = courseArr.find(el => {
                    return el.latest === 1
                })

                // 渲染新课程
                renderNewCourse(newCourseArr)

                // 找到旧课程
                oldCourseArr = courseArr.filter(el => {
                    return el.latest === 0
                })

                // 渲染旧课程
                renderOldCourse(oldCourseArr);
            }



        }


    })


    let newCourse = document.querySelector("#newCourse");
    let oldCourse = document.querySelector("#oldCourse");
    // 渲染函数
    function renderNewCourse(newCourseArr) {


        let div = document.createElement('div')
        let str =
            `
              <p> 最新课程 </p>
            <a href="./projectDes.html?id=${newCourseArr.courseId}">
                <img src="${BASE_URL}${newCourseArr.imgurl}" alt="">
                <div class="fs14">
                    <span class="fs16">${newCourseArr.name}</span>
                    <p class="c666 lit-warp">${newCourseArr.desc}</p>
                </div>
            </a>`
        div.innerHTML = str

        newCourse.appendChild(div)

    }

    // 渲染函数
    function renderOldCourse(oldCourseArr) {
        console.log(oldCourseArr);

        let div = document.createElement('div')
        let strEle = ""
        oldCourseArr.forEach(el => {
            strEle +=
                `<p>课程</p>
                 <a href="./projectDes.html?id=${el.courseId}" >
                    <img src="${BASE_URL}${el.imgurl}" alt = "" >
                    <span class="fs20">${el.name}</span>
                    <p class="c666 fs14 lit-warp">${el.desc}</p>
                </a>`

        })
        div.innerHTML = strEle
        oldCourse.appendChild(div)

    }










})